# -*- coding: utf-8 -*-

import io

class JSAndERP():
    def __init__(self):
        self.sqlArr=[]
        self.dataTxtArr=[]
        self.idsmDataArr=[]

    def getJSDataFromFile(self):
        #print("读取金税文件内容")
        try:
            file = io.open(ur'E:/工作资料/客户资料/航天开票/鸿都/金税导入/金税导入文件/商品编码(全)_20170711_myop.xml', 'r', encoding='gbk')
            while True:
                lines = file.readlines(10000)
                if not lines:
                    break
                for line in lines:
                    #print(line)
                    if line.startswith('/'):
                        continue

                    data = line.split('~~')
                    datalen=len(data)
                    if datalen == 17:
                        self.dataTxtArr.append(line)
                        self.idsmDataArr.append((data[0],data[11]))
        finally:
            if file:
                file.close()

    def getJSSMSql(self):
        for item in self.idsmDataArr:
            sql = "update t_base_goods_info set jstaxsortid='%s',jsgoodsmodifytime=now() where jsgoodsid='%s';" % (item[1],item[0])
            self.sqlArr.append(sql)

    def generateJSSMUpdateSql(self):
        self.getJSDataFromFile()
        self.getJSSMSql()
        try:
            filepath = ur'E:/工作资料/客户资料/航天开票/鸿都/金税导入/金税导入文件/更新ERP金税税目分类' + unicode(jsERP.getDatetimeStr()) + u'.sql'
            file=io.open(filepath,'w',encoding='utf-8')
            sqlcont="\n".join(self.sqlArr)
            file.write(sqlcont)
            print("write sql sucessful")
        finally:
            if file:
                file.close()


    def showDataText(self):
        print("".join(self.dataTxtArr))

    def showIdSMData(self):
        for itm in self.idsmDataArr:
            print(itm)

    def showUpdateSql(self):
        print("sql len"+str(len(self.sqlArr)))
        print("\n".join(self.sqlArr))

    def getDatetimeStr(self):
        import time
        timestamp = time.time()
        timestruct = time.localtime(timestamp)
        return time.strftime('%Y%m%d%H%M%S', timestruct)

if __name__=="__main__":
    jsERP=JSAndERP()
    jsERP.generateJSSMUpdateSql()

